package pres.lnk.learn.designpattern.p_interpreter;

import pres.lnk.learn.designpattern.p_interpreter.impl.AndExpression;
import pres.lnk.learn.designpattern.p_interpreter.impl.ContainsExpression;
import pres.lnk.learn.designpattern.p_interpreter.impl.OrExpression;

/**
 * <p>解释器模式</p>
 * @Author lnk
 * @Date 2018/5/4
 */
public class Main {
    public static void main(String[] args) {
        Expression isMale = new ContainsExpression("男");
        Expression isFemale = new ContainsExpression("女");

        Expression isSingleDog = new ContainsExpression("单身");
        Expression isMarried = new ContainsExpression("已婚");

        Expression maleAndMarried = new AndExpression(isMale, isMarried);
        Expression femaleOrSingleDog = new OrExpression(isFemale, isSingleDog);

        String user1 = "张三 男 已婚";
        String user2 = "李雪 女 单身";

        System.out.println("张三是已婚男性吗？" + maleAndMarried.interpret(user1));
        System.out.println("李雪是男性吗？" + isMale.interpret(user2));

        System.out.println("单身或是女性的有：");
        String[] users = {user1, user2};
        for (int i = 0; i < users.length; i++) {
            if(femaleOrSingleDog.interpret(users[i])){
                System.out.println(users[i]);
            }
        }
    }
}
